<template>
	<view class="root">
		<view class="uni-padding-wrap">
			<view class="uni-title">{{title}}</view>
			<button class="mini-btn" type="primary" @click="addInfo()">提交</button>
			
			<view class="uni-title" v-if="key==='0'">
				<view class="uni-title">
					<uni-section title="计划状态" type="line"></uni-section>
				</view>
				<view class="uni-form-item uni-column">
					<uni-data-select
						v-model="form.status"
						:localdata="dictItems"
					  ></uni-data-select>
					</uni-section>
				</view>
				
				<view class="uni-title uni-form-item uni-column">
					<uni-section title="计划内容" type="line"></uni-section>
				</view>
				<view class="uni-textarea uni-form-item uni-column">
					<textarea  auto-height v-model="form.description"/>
				</view>
			</view>
			
	
			
			<view v-if="key==='1'">
				<view class="uni-title">
					<uni-section title="日期类型" type="line"></uni-section>
				</view>
				<view class="uni-form-item uni-column">
					<uni-data-select
						v-model="form.type"
						:localdata="dictItems"
					  ></uni-data-select>
					</uni-section>
				</view>
				<uni-section title="到期时间" type="line"></uni-section>
				<view class="uni-form-item uni-column">
					<uni-datetime-picker type="datetime" v-model="form.endtime" />
				</view>
				
				<uni-section title="日期内容" type="line"></uni-section>
				<view class="uni-form-item uni-column">
					<!-- <view class="title">到期剩余时长</view> -->
					<input class="uni-input"  type="text" v-model="form.name" />
				</view>
			</view>
			
			
			<view v-if="key==='2'">
				<uni-section title="购物内容" type="line"></uni-section>
				<view class="uni-form-item uni-column">
					<input class="uni-input" type="text" v-model="form.name" placeholder="请输入"/>
				</view>
				<uni-section title="购物状态" type="line"></uni-section>
				<view class="uni-form-item uni-column">
					<uni-data-select
						v-model="form.status"
						:localdata="dictItems"
					  ></uni-data-select>
					</uni-section>
				</view>
			</view>
			
			<view v-if="key==='3'">
				<view class="uni-title">登录凭证</view>
				<uni-list-item  title="创建时间" :note="obj.createtime" ></uni-list-item>
				<uni-list-item  title="更新时间" :note="obj.updatetime" ></uni-list-item>
				<view class="uni-form-item uni-column">
					<view class="title">凭证内容</view>
					<input class="uni-input" disabled="true" type="text" :value="obj.name" />
				</view>
				<uni-section title="用户名" type="line"></uni-section>
				<view class="uni-form-item uni-column">
					<!-- <view class="title">到期剩余时长</view> -->
					<input class="uni-input" disabled="true" type="text" :value="obj.username" />
				</view>
				<uni-section title="密码" type="line"></uni-section>
				<view class="uni-form-item uni-column">
					<!-- <view class="title">到期剩余时长</view> -->
					<input class="uni-input" disabled="true" type="text" :value="obj.password" />
				</view>
				<uni-section title="网址" type="line"></uni-section>
				<view class="uni-form-item uni-column">
					<!-- <view class="title">到期剩余时长</view> -->
					<!-- <input class="uni-input" disabled="true" type="text" :value="obj.url" /> -->
					<uni-link  class="uni-input" :href="obj.url" :text="obj.url"></uni-link>
				</view>
			</view>
			
			<!-- 网址 -->
			<view v-if="key==='4'">
				<view class="uni-title">网址信息</view>
				<uni-list-item  title="创建时间" :note="obj.createtime" ></uni-list-item>
				<uni-list-item  title="更新时间" :note="obj.updatetime" ></uni-list-item>
				<view class="uni-form-item uni-column">
					<view class="title">网址</view>
					<input class="uni-input" disabled="true" type="text" :value="obj.website" />
				</view>
				<uni-section title="类型" type="line"></uni-section>
				<view class="uni-form-item uni-column">
					<!-- <view class="title">到期剩余时长</view> -->
					<input class="uni-input" disabled="true" type="text" :value="getLabel(obj.belongs)" />
				</view>
				<uni-section title="网址" type="line"></uni-section>
				<view class="uni-form-item uni-column">
					<!-- <view class="title">到期剩余时长</view> -->
					<!-- <input class="uni-input" disabled="true" type="text" :value="obj.url" /> -->
					<uni-link  class="uni-input" :href="obj.url" :text="obj.url"></uni-link>
				</view>
				<uni-section title="备注" type="line"></uni-section>
				<view class="text-box" scroll-y="true">
					<text>{{obj.remark}}</text>
				</view>
			</view>
			
			<!-- 媒体 -->
			<view v-if="key==='5'">
				
				<uni-section title="媒体名称" type="line"></uni-section>
				<view class="uni-form-item uni-column">
					<input class="uni-input"  type="text" v-model="form.name" />
				</view>
				
				<uni-section title="来源" type="line"></uni-section>
				<view class="uni-form-item uni-column">
					<input class="uni-input"  type="text" v-model="form.type" />
				</view>
				<uni-section title="类型" type="line"></uni-section>
				<view class="uni-form-item uni-column">
					<uni-data-select
						v-model="form.origin"
						:localdata="dictItems"
					  ></uni-data-select>
					</uni-section>
					<!-- <view class="title">到期剩余时长</view> -->
					
				</view>
				<uni-section title="其他" type="line"></uni-section>
				<view class="uni-form-item uni-column">
					<!-- <view class="title">到期剩余时长</view> -->
					<!-- <input class="uni-input" disabled="true" type="text" :value="obj.url" /> -->
					<input class="uni-input"  type="text" v-model="form.other" />
				</view>
			</view>
			
			
		</view>
	</view>
</template>
<script>
	import { request } from '@/common/util.js'
	import { listItem,addPlanInfo,addDateInfo, addShoppingInfo,addMediaInfo } from '@/common/api'
	export default {
		data() {
			return {
				title: '',
				key: 0,
				id: '',
				obj: {},
				dictItems: [],
				dictItemQuery: [
					{ 'value': 'PlanStatus' },
					{ 'value': 'EndType' },
					{ 'value': 'ShoppingStatus' },
					{ 'value': 'ApiStatus' },
					{ 'value': 'WebSiteType' },
					{ 'value': 'MediaType' }
				],
				/* 新增的表单内容 */
				form: {},
			}
		},
		onLoad(e) {
			console.log("详情页加载-新增",e)
			this.key = e.key;
			this.title = e.title || '';
			this.init(this.key);
			console.log(this.key)
		},
		methods: {
			addInfo(){
				console.log(this.key)
				switch (this.key) {
					case '5':
						console.log("新增媒体信息",this.form)
						addMediaInfo(this.form).then(res=>{
							console.log(res)
							if(res.message === '成功'){
								uni.showToast({
									title: res.message,
									icon: 'none'
								})
								this.navigateBack();
							}else{
								uni.showToast({
									title: res.message,
									icon: 'none'
								})
							}
						})
						break;
					case '0':
						// 如果 expression === value2，执行这里的代码
						console.log("新增计划信息",this.form)
						addPlanInfo(this.form).then(res=>{
							console.log(res)
							if(res.message === '成功'){
								uni.showToast({
									title: res.message,
									icon: 'none'
								})
								this.navigateBack();
							}else{
								uni.showToast({
									title: res.message,
									icon: 'none'
								})
							}
						})
						break;
					case '1':
						// 如果 expression === value2，执行这里的代码
						console.log("新增日期信息",this.form)
						addDateInfo(this.form).then(res=>{
							console.log(res)
							if(res.message === '成功'){
								uni.showToast({
									title: res.message,
									icon: 'none'
								})
								this.navigateBack();
							}else{
								uni.showToast({
									title: res.message,
									icon: 'none'
								})
							}
						})
						break;
					case '2':
						// 如果 expression === value2，执行这里的代码
						console.log("新增购物信息",this.form)
						addShoppingInfo(this.form).then(res=>{
							console.log(res)
							if(res.message === '成功'){
								uni.showToast({
									title: res.message,
									icon: 'none'
								})
								this.navigateBack();
							}else{
								uni.showToast({
									title: res.message,
									icon: 'none'
								})
							}
						})
						break;
					// 可以继续添加更多的 case
					default:
						// 如果没有任何 case 匹配，执行这里的代码
				}
				//this.newsList[index1].data.splice(index2, 1);//不需要刷新，直接从数组中删除。
				
			},
			change(e) {
			  console.log("e:", e);
			},
			getLabel(value) {
			  // 使用 find() 方法遍历数组，找到匹配的对象
			  const found = this.dictItems.find(item => item.value === value)
			  			
			  // 如果找到匹配的项，返回 label，否则返回一个提示
			  return found ? found.label : ''
			},
			//初始化字典内容
			init(key) {
			  console.log(key)
			  listItem(this.dictItemQuery[key]).then(response => {
							console.log("listItem",response)
				response.obj.map(item=>{
					item.text = item.label
					this.dictItems.push(item)
				})			
				//this.dictItems = response.obj
			  })
			  
			},
			//返回上一页面
			navigateBack() {
				uni.navigateBack();
				/* uni.redirectTo({
					url: '/pages/tabBar/core/core'
				}); */
			},
		}
	}
</script>
<style>

</style>
